package com.zsc.shixun.controller;


import com.zsc.shixun.common.CommonResult;
import com.zsc.shixun.common.ResultUtil;
import com.zsc.shixun.model.article.ArticleCreateVO;
import com.zsc.shixun.model.article.ArticleUpdateVO;
import com.zsc.shixun.service.IArticleService;
import com.zsc.shixun.utils.LoginMessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 文章管理 前端控制器
 * </p>
 *
 * @author ZWYZY
 * @since 2020-06-06
 */
@Controller
@Validated
@RequestMapping("/article")
public class ArticleController {
    private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);
    @Autowired
    private IArticleService articleService;

    @Autowired
    private LoginMessageUtils loginMessageUtils;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        request.setAttribute("articleVO", articleService.getData(loginMessageUtils.getMessage().getLoginUser().getId()));
        return "admin/article/index";
    }

    /**
     * 获取文章列表
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    @Secured(value = "ROLE_article_select")
    public CommonResult getData() {
        return ResultUtil.success(articleService.getData(loginMessageUtils.getMessage().getLoginUser().getId()));
    }

    /**
     * 修改文章
     *
     * @param articleUpdateVO
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @Secured(value = "ROLE_article_update")
    public CommonResult update(@Validated @ModelAttribute ArticleUpdateVO articleUpdateVO) {
        System.out.println("----修改文章vo-------");
        System.out.println(articleUpdateVO.getId());
        System.out.println(articleUpdateVO.getUserId());
        articleService.update(articleUpdateVO);
        return ResultUtil.success("修改文章成功哦，亲");
    }

    /**
     * 删除文章
     *
     * @param id 文章id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    @Secured(value = "ROLE_article_delete")
    public CommonResult delete(@NotNull(message = "id不能为空") @RequestParam(required = false) Long id) {
        articleService.delete(id, loginMessageUtils.getMessage().getLoginUser().getId());
        return ResultUtil.success("删除文章成功哦，亲");
    }

    /**
     * 创建文章
     *
     * @param articleCreateVO
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    @Secured(value = "ROLE_article_insert")
    public CommonResult insert(@Validated @ModelAttribute ArticleCreateVO articleCreateVO) {
        articleService.insert(articleCreateVO, loginMessageUtils.getMessage().getLoginUser().getId());
        return ResultUtil.success("创建文章成功哦，亲");
    }


    /**
     * 点赞
     *
     * @param id  文章id
     * @param aid 文章作者id
     * @return
     */
    @GetMapping("/liking")
    @ResponseBody
    @Secured(value = "ROLE_article_like")
    public CommonResult liking(@NotNull(message = "id不能为空") @RequestParam(required = false) Long id, @NotNull(message = "文章作者id不能为空") @RequestParam(required = false) Long aid) {
        articleService.liking(id, loginMessageUtils.getMessage().getLoginUser().getId(), aid);
        return ResultUtil.success("点赞成功哦，亲");
    }

}
